<?php
namespace app\printing\controller;
use think\Request;
use think\Exception;
use app\common\controller\Common;
class Printing extends Common
{	
	protected $printing = [];//获取配置
	protected $model = null;
	protected $afterActionList = [
        'addOperLog'  =>  ['only'=>'edit'],
    ];

	public function _initialize(){
		parent::_initialize();
		$this->printing = config('repair.printing');
		$this->model = new \app\printing\model\Printing;
	}

	/**
	* 打印管理
	*/
	public function index()
	{
		if(Request()->isGet()){
			if(Request()->isAjax()){

				$data = Request()->param('');
				return $this->getList($data);
			}
			return $this->fetch('');
		}
	}

	/**
	* 打印设置
	*/
	public function edit($ids = null)
	{
		$row = $this->model->where('state',$ids)->find();
		if(Request()->isGet()){
			$content = $this->getContent($ids);
			$this->assign('content',$content);

			$title = $this->printing['state'][$ids];
			$this->assign('title',$title);

			$row->content = $row->content ? explode(',', $row->content) : [];
			$this->assign('row',$row);
			return $this->fetch('');
		}
		if(Request()->isAjax()){
			$data = Request()->param('');
			$this->log_id = $ids;
			try{
				$model = $row ? $row : $this->model;
				// print($model);
				$model->is_show = $data['is_show'];
				$model->content = $data['field'] ? implode(',', $data['field']) : '';
				$model->state = $ids;
				$model->allowField(true)->save($data);
				$this->success(config('status.SUCCESS_ONE'));
			}catch(Exception $e){
				$this->error(config('status.ERROR_FIVE').$e->getMessage());
			}
			
		}
	}

	/**************************************** 私有 ****************************************************/
	/**
	* 获取打印内容
	*/
	private function getContent($state)
	{
		switch($state){
			case 1: return $this->printing['content_1'];break;
			case 2: return $this->printing['content_2'];break;
			case 3: return $this->printing['content_3'];break;
			case 4: return $this->printing['content_4'];break;
			case 5: return $this->printing['content_5'];break;
			case 6: return $this->printing['content_6'];break;
			default: $this->error(config('status.ERROR_SIX'));
		}
	}
}